//-----------------------------------------------------------------------
// <copyright file="Capabilities.cs" company="Microsoft">
//     Copyright (c) Microsoft Corporation.  All rights reserved.
// </copyright>
//-----------------------------------------------------------------------

namespace Microsoft.Live.Messenger
{
    using System;
    using System.ComponentModel;
    using Microsoft.Live.Core;

    /// <summary>
    /// Represents the capabilities of an address or endpoint.
    /// </summary>
    public sealed class Capabilities : INotifyPropertyChanged
    {
        private readonly MessengerEntity entity;
        private readonly IMAddress address;

        /// <summary>
        /// Initializes a new instance of the <see cref="Capabilities"/> class.
        /// </summary>
        /// <param name="address">The associated address.</param>
        internal Capabilities(object obj, IMAddress address)
        {
            this.entity = new MessengerEntity(obj);
            this.entity.AttachEvent("PropertyChanged", this.OnPropertyChanged);

            this.address = address;
        }

        /// <summary>
        /// Gets whether the address or endpoint is online via a mobile device.
        /// </summary>
        public bool IsOnlineViaMobileDevice
        {
            get { return this.entity.GetProperty<bool>("IsOnlineViaMobileDevice"); }
        }

        /// <summary>
        /// Gets whether the address or endpoint is online via the web.
        /// </summary>
        public bool IsOnlineViaWeb
        {
            get { return this.entity.GetProperty<bool>("IsOnlineViaWeb"); }
        }

        /// <summary>
        /// Gets the client version for the address or endpoint.
        /// </summary>
        public Version Version
        {
            get { return this.entity.GetVersionProperty("Version"); }
        }

        /// <summary>
        /// Gets whether the address or endpoint supports multiparty conversations.
        /// </summary>
        public bool SupportsMultipartyConversations
        {
            get { return this.entity.GetProperty<bool>("SupportsMultipartyConversations"); }
        }

        /// <summary>
        /// Gets whether the address supports the provided message type. 
        /// </summary>
        /// <param name="messageType"></param>
        /// <returns></returns>
        public bool SupportsMessageType(MessageType messageType)
        {
            return (bool)this.entity.Invoke("SupportsMessageType", messageType);
        }

        #region INotifyPropertyChanged Members

        /// <summary>
        /// Occurs when a property value changes.
        /// </summary>
        public event PropertyChangedEventHandler PropertyChanged;

        #endregion

        private void OnPropertyChanged(object sender, MessengerEventArgs e)
        {
            this.OnPropertyChanged(
                new PropertyChangedEventArgs(
                    e.Entity.GetProperty<string>("PropertyName")));
        }

        private void OnPropertyChanged(PropertyChangedEventArgs e)
        {
            if (this.PropertyChanged != null)
            {
                this.PropertyChanged(this, e);
            }
        }
    }
}
